package com.bob.usercenter.controller;

import cn.hutool.core.util.StrUtil;
import com.bob.usercenter.common.ErrorCode;
import com.bob.usercenter.common.Result;
import com.bob.usercenter.constant.UserConstant;
import com.bob.usercenter.entity.User;
import com.bob.usercenter.entity.request.UserLoginDTO;
import com.bob.usercenter.entity.request.UserRegisterDTO;
import com.bob.usercenter.exception.BusinessException;
import com.bob.usercenter.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.bob.usercenter.constant.UserConstant.USER_LOGIN_STARE;

/**
 * @Author Bob
 * @Date：2024-01-19 10:12
 * @description：
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;


    /**
     * 注册接口
     */
    @PostMapping("/register")
    public Result<Long> register(@RequestBody UserRegisterDTO userRegisterDTO){
        if(!StrUtil.isAllNotBlank(userRegisterDTO.getUserAccount(), userRegisterDTO.getPassword(), userRegisterDTO.getCheckPassword())){
            throw new BusinessException(ErrorCode.PARAMS_IS_EMPTY_ERROR);
        }
        int data = userService.register(userRegisterDTO.getUserAccount(), userRegisterDTO.getPassword(), userRegisterDTO.getCheckPassword());
        return Result.success(data);
    }

    /**
     * 登录接口
     */
    @PostMapping("/login")
    public Result<User> login(@RequestBody UserLoginDTO userLoginDTO, HttpServletRequest request){
        log.info("登录接口被调用，参数为:{},{}",userLoginDTO,request);
        if(!StrUtil.isAllNotBlank(userLoginDTO.getUserAccount(),userLoginDTO.getPassword())){
            throw new BusinessException(ErrorCode.PARAMS_IS_EMPTY_ERROR);
        }
        User data = userService.login(userLoginDTO.getUserAccount(), userLoginDTO.getPassword(), request);
        log.info("服务端返回的信息：{}",Result.success(data));
        return Result.success(data);
    }
    /**
     * 查询用户接口
     */
    @GetMapping("selectList")
    public Result<List<User>> selectList(@RequestParam(required = false) String username,HttpServletRequest request){
        log.info("正在查询用户信息，参数为:{},{}",request);
        List<User> data = userService.queryUsers(username, request);
        return Result.success(data);
    }
    /*@GetMapping("selectList")
    public List<User> selectList(@RequestParam("current") String current,@RequestParam("pageSize") String pageSize,@RequestParam(value = "username",required = false) String username,  HttpServletRequest request){
        log.info("正在查询用户信息，参数为:{},{}",current,pageSize);
        return userService.queryUsersByPage(current, pageSize, username, request);
    }*/


    /**
     * 删除用户接口
     */
    @DeleteMapping("/delete")
    public Result<Boolean> delete(Long id,HttpServletRequest request){
        return Result.success(userService.deleteUser(id,request));
    }

    /**
     * 获取当前用户
     */
    @GetMapping("/current")
    public Result<User> current(HttpServletRequest request){
        return Result.success(userService.current(request));
    }

    /**
     * 退出登陆
     */
    @PostMapping("outLogin")
    public void outLogin(HttpServletRequest request){
        request.getSession().removeAttribute(USER_LOGIN_STARE);
    }

    /**
     * 通过id获取用户
     */


}
